from django.db import models


# Create your models here.


# 用户表
class UserInfo(models.Model):
    username = models.CharField(max_length=18)
    password = models.CharField(max_length=32)
    email = models.EmailField()
    telephone = models.CharField(max_length=12)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return self.username


# 吐槽表
class DB_tucao(models.Model):
    user = models.CharField(max_length=30)
    text = models.CharField(max_length=100000)
    ctime = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.text + str(self.ctime)


# 传送门表
class DB_Gateway(models.Model):
    name = models.CharField(max_length=30)
    href = models.CharField(max_length=100)

    def __str__(self):
        return self.name + self.href


# 项目表
class DB_project(models.Model):
    name = models.CharField(max_length=30, null=True)  # 项目名称
    remark = models.CharField(max_length=30, null=True)  # 项目备注
    user = models.CharField(max_length=30, null=True)  # 项目创建者
    user_id = models.CharField(max_length=10,null=True)
    other_user = models.CharField(max_length=30, null=True)
    # 项目其他创建者

    def __str__(self):
        return self.name + self.user


# 接口表
class DB_apis(models.Model):
    # project_id = models.CharField(max_length=10,null=True) #项目id
    project_id = models.ForeignKey(to="DB_project", to_field="id", on_delete=models.CASCADE)
    name = models.CharField(max_length=100, null=True)  # 接口名字
    api_method = models.CharField(max_length=10, null=True)  # 请求方式
    api_url = models.CharField(max_length=1000, null=True)  # url
    api_header = models.CharField(max_length=1000, null=True)  # 请求头
    api_login = models.CharField(max_length=10, null=True)  # 是否带登陆态
    api_host = models.CharField(max_length=100, null=True)  # 域名
    des = models.CharField(max_length=100, null=True)  # 描述
    body_method = models.CharField(max_length=20, null=True)  # 请求体编码格式
    api_body = models.CharField(max_length=1000, null=True)  # 请求体
    result = models.TextField(null=True)  # 返回体 因为长度巨大，所以用大文本方式存储
    sign = models.CharField(max_length=10, null=True)  # 是否验签
    file_key = models.CharField(max_length=50, null=True)  # 文件key
    file_name = models.CharField(max_length=50, null=True)  # 文件名
    public_header = models.CharField(max_length=1000, null=True)  # 全局变量-请求头
    last_body_method = models.CharField(max_length=1000, null=True)
    last_api_body = models.CharField(max_length=1000, null=True)

    def __str__(self):
        return self.name


# 首页请求记录表
class DB_apis_log(models.Model):
    user_id = models.CharField(max_length=10, null=True)  # 所属用户id
    api_method = models.CharField(max_length=10, null=True)  # 请求方式
    api_url = models.CharField(max_length=1000, null=True)  # url
    api_header = models.CharField(max_length=1000, null=True)  # 请求头
    api_login = models.CharField(max_length=10, null=True)  # 是否带登陆态
    api_host = models.CharField(max_length=100, null=True)  # 域名
    body_method = models.CharField(max_length=20, null=True)  # 请求体编码格式
    api_body = models.CharField(max_length=1000, null=True)  # 请求体

    sign = models.CharField(max_length=10, null=True)  # 是否验签
    file_key = models.CharField(max_length=50, null=True)  # 文件key
    file_name = models.CharField(max_length=50, null=True)  # 文件名

    def __str__(self):
        return self.api_url


# 大用例表
class DB_cases(models.Model):
    project_id = models.CharField(max_length=30, null=True)  # 所属项目id
    name = models.CharField(max_length=50, null=True)  # 大用例名
    creater = models.CharField(max_length=50, null=True)  # 创建人

    def __str__(self):
        return self.name


# 小用例表
class DB_step(models.Model):
    Case_id = models.CharField(max_length=10, null=True)  # 所属大用例id
    name = models.CharField(max_length=50, null=True)  # 步骤名字
    index = models.IntegerField(null=True)  # 执行步骤
    api_method = models.CharField(max_length=10, null=True)  # 请求方式
    api_url = models.CharField(max_length=1000, null=True)  # url
    api_host = models.CharField(max_length=100, null=True)  # host
    api_header = models.CharField(max_length=1000, null=True)  # 请求头
    api_body_method = models.CharField(max_length=10, null=True)  # 请求体编码类型
    api_body = models.CharField(max_length=10, null=True)  # 请求体
    get_path = models.CharField(max_length=500, null=True)  # 提取返回值-路径法
    get_zz = models.CharField(max_length=500, null=True)  # 提取返回值-正则
    assert_zz = models.CharField(max_length=500, null=True)  # 断言返回值-正则
    assert_qz = models.CharField(max_length=500, null=True)  # 断言返回值-全文检索存在
    assert_path = models.CharField(max_length=500, null=True)  # 断言返回值-路径法
    mock_res = models.CharField(max_length=500, null=True)  # mock返回值
    public_header = models.CharField(max_length=1000, null=True)  # 全局请求头
    api_login = models.CharField(max_length=100,null=True) # 登录态

    def __str__(self):
        return self.name


# mock 功能模块的表

class Interface(models.Model):
    # 接口表

    mode_choice = (
        ("GET", "GET"),
        ("POST", "POST"),
        ("PUT", "PUT"),
        ("DELETE", "DELETE"),
        ("PATCH", "PATCH"),
    )
    # 请求方式枚举
    # 第一个元素是存储在数据库里面的值
    # 第二个元素是页面显示的值

    interface_name = models.CharField(
        max_length=32, verbose_name="接口名称",
        help_text="请输入接口名称", db_index=True)
    # 接口名称，并创建索引
    interface_url = models.CharField(
        max_length=255, verbose_name="接口地址", help_text="请输入接口地址")
    # 接口地址
    request_mode = models.CharField(
        choices=mode_choice, max_length=11,
        verbose_name="请求方式", default="GET",
        help_text="请选择请求方式")
    # 请求方式
    create_time = models.DateTimeField(
        auto_now_add=True, blank=True, null=True, verbose_name="创建时间")
    # 创建时间
    update_time = models.DateTimeField(
        auto_now=True, blank=True, null=True, verbose_name="修改时间")

    # 修改时间

    class Meta:
        db_table = "interface"
        verbose_name = "接口列表"
        verbose_name_plural = "接口列表"

    def __str__(self):
        return self.interface_name


class Scene(models.Model):
    # 场景表

    body_choice = (
        ("x-www-form-urlencoded", "application/x-www-form-urlencoded"),
        ("json", "application/json"),
        ("form-data", "multipart/form-data"),
        ("xml", "text/xml"),
    )
    # 请求体类型枚举

    scene_group = models.ForeignKey(
        Interface, on_delete=models.CASCADE,
        verbose_name="接口", related_name="interfaces",
        help_text="请选择接口")
    # 外键，关联接口id
    scene_name = models.CharField(
        max_length=32, verbose_name="场景名称",
        help_text="请输入场景名称", db_index=True)
    # 场景名称，并创建索引
    request_head = models.TextField(
        verbose_name="请求头", blank=True, null=True,
        help_text="请输入字典格式的请求头", default="")
    # 请求头
    request_parameter = models.TextField(
        verbose_name="请求参数", blank=True, null=True,
        help_text="请输入字典格式的请求参数", default="")
    # 请求参数
    body_type = models.CharField(
        choices=body_choice, max_length=21,
        blank=True, null=True,
        verbose_name="请求体类型", default="x-www-form-urlencoded",
        help_text="请选择请求体类型")
    # 请求体类型
    request_body = models.TextField(
        verbose_name="请求体", blank=True, null=True,
        help_text="请输入浏览器原生表单、json、文件或xml格式的请求体", default="")
    # 请求体
    response_result = models.TextField(
        verbose_name="响应结果", blank=True, null=True,
        help_text="请输入字典格式的响应结果", default="")
    # 响应结果
    create_time = models.DateTimeField(
        auto_now_add=True, blank=True, null=True, verbose_name="创建时间")
    # 创建时间
    update_time = models.DateTimeField(
        auto_now=True, blank=True, null=True, verbose_name="修改时间")

    # 修改时间

    class Meta:
        db_table = "scene"
        verbose_name = "场景列表"
        verbose_name_plural = "场景列表"

    def __str__(self):
        return self.scene_name


# 全局请求头
class DB_project_header(models.Model):
    project_id = models.CharField(max_length=10, null=True)  # 项目id
    name = models.CharField(max_length=20, null=True)  # 请求头变量名字
    key = models.CharField(max_length=20, null=True)  # 请求头key
    value = models.TextField(null=True)  # 请求头value 因为cookie可能非常大，这里采用大文本存储

    def __str__(self):
        return self.name


# 域名表
class DB_host(models.Model):
    host = models.CharField(max_length=500)
    des = models.CharField(max_length=500)

    def __str__(self):
        return self.host


# 全局域名
class DB_project_host(models.Model):
    project_id = models.CharField(max_length=10, null=True)  # 项目id
    name = models.CharField(max_length=20, null=True)
    host = models.TextField(null=True)

    def __str__(self):
        return self.name


# 登录态
class DB_login(models.Model):
    project_id = models.CharField(max_length=10, null=True)
    api_method = models.CharField(max_length=10, null=True)
    api_url = models.CharField(max_length=1000, null=True)
    api_header = models.CharField(max_length=1000, null=True)
    api_host = models.CharField(max_length=1000, null=True)
    body_method = models.CharField(max_length=1000, null=True)
    api_body = models.CharField(max_length=1000, null=True)
    sign = models.CharField(max_length=1000, null=True)  # 是否验签
    set = models.CharField(max_length=1000, null=True)  # 提取设置

    def __str__(self):
        return self.project_id


# 全局变量表
class DB_global_data(models.Model):
    name = models.CharField(max_length=50,null=True)
    user_id = models.CharField(max_length=10,null=True)
    data = models.TextField(null=True)